import json
import numpy as np
import csv

class Coord3D:
    def __init__(self):
        self._data = []
        self.width = 1024
        self.height = 768

    def setSize(self,w,h):
        self.width = w
        self.height = h

    def getCenter(self):
        # avg = np.mean(self._data,axis=(0,1))

        # avg = np.array([-2096164.8684110371, 3620763.961749195,4805369.761587891], dtype=np.float32)
        avg = np.array([0, 0, 0], dtype=np.float32)
        return avg

    # def loadcsv(self,path):
    #     data = np.zeros([self.height, self.width, 3], dtype=np.float)
    #     with open(path, 'r', encoding='utf-8') as f:
    #         rows = csv.reader(f)
    #         idx = 0
    #         for row in rows:
    #             pa = []
    #             pa.append(float(row[0]))
    #             pa.append(float(row[1]))
    #             pa.append(float(row[2]))
    #             pa[2] = pa[2] * -1
    #             h = int(idx / self.width)
    #             w = idx % self.width
    #             data[h,w] = pa
    #             idx += 1
    #     self._data = data
    def loadcsv(self,path):
        #读入数据坐标系为CESIUM坐标系(Y向上，Z向内)
        #需要转换为OPENCV坐标系(Y向下，Z向外)下


        data = np.zeros([self.width, self.height, 3], dtype=np.float32)
        with open(path, 'r', encoding='utf-8') as f:
            rows = csv.reader(f)
            idx = 0
            for row in rows:
                pa = []
                pa.append(float(row[0]))
                pa.append(float(row[1]))
                pa.append(float(row[2]))
                pa[1] = pa[1] * -1
                pa[2] = pa[2] * -1
                h = int(idx / self.width)
                w = idx % self.width
                data[w, h] = pa
                idx += 1
        self._data = data

    def loadFile(self,path):
        data = np.zeros([self.width,self.height,3],dtype=np.float32)
        idx = 0
        with open(path, 'r') as load_f:
            xyzlist = json.load(load_f)
            for p in xyzlist:
                pa = []
                pa.append(p["x"])
                pa.append(p["y"])
                pa.append(p["z"])
                h = int(idx / self.width)
                w = idx % self.width
                # h = self.height - 1 - h
                data[w,h] = pa
                idx += 1

        # data = np.array(list,dtype='float')
        # data = data.reshape((1024,-1,3))
        self._data = data

    # def loadFile(self,path):
    #     list = []
    #     with open(path, 'r') as load_f:
    #         xyzlist = json.load(load_f)
    #         for p in xyzlist:
    #             pa = []
    #             pa.append(p["x"])
    #             pa.append(p["y"])
    #             pa.append(p["z"])
    #             list.append(pa)
    #     data = np.array(list,dtype='float')
    #     data = data.reshape((1024,-1,3))
    #     self._data = data
    def getCoord3d(self,x,y):
        #读入图像原点为左下角
        #OPENCV图像原点为左上角
        y = self.height - y - 1
        return self._data[x,y].copy()
        # idx = y * self.width +x
        # return self._data[idx]
